/**
 * Copyright (c) 2018-2099, Chill Zhuang 庄骞 (bladejava@qq.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.restaurant.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.restaurant.entity.Area;
import org.springblade.modules.restaurant.service.IAreaService;
import org.springblade.modules.restaurant.vo.AreaVO;
import org.springblade.modules.restaurant.wrapper.AreaWrapper;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 区域表 控制器
 *
 * @author Blade
 * @since 2025-01-21
 */
@RestController
@AllArgsConstructor
@RequestMapping("/restaurant/area")
@Tag(name = "区域表", description = "区域表接口")
public class AreaController extends BladeController {

	private IAreaService areaService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description = "传入area")
	public R<Area> detail(Area area) {
		Area detail = areaService.getOne(Condition.getQueryWrapper(area));
		return R.data(detail);
	}

	/**
	 * 分页 区域表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页", description = "传入area")
	public R<List<AreaVO>> list(@ParameterObject Area area) {
		LambdaQueryWrapper<Area> wrapper = new LambdaQueryWrapper<>();
		if (Func.isNotEmpty(area.getAreaName())) {
            wrapper.like(Area::getAreaName, area.getAreaName());
        }
		List<Area> list = areaService.list(wrapper);
		return R.data(AreaWrapper.build().listNodeVO(list));
	}

	@GetMapping("/tree")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "树形结构", description = "树形结构")
	public R<List<AreaVO>> tree(Integer type) {
		List<AreaVO> tree = areaService.tree(type);
		return R.data(tree);
	}
	/**
	 * 自定义分页 区域表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "分页", description = "传入area")
	public R<IPage<AreaVO>> page(@ParameterObject AreaVO area, @ParameterObject Query query) {
		IPage<AreaVO> pages = areaService.selectAreaPage(Condition.getPage(query), area);
		return R.data(pages);
	}

	/**
	 * 新增 区域表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "新增", description = "传入area")
	public R save(@Valid @RequestBody Area area) {
		return R.status(areaService.save(area));
	}

	/**
	 * 修改 区域表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "修改", description = "传入area")
	public R update(@Valid @RequestBody Area area) {
		return R.status(areaService.updateById(area));
	}

	/**
	 * 新增或修改 区域表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "新增或修改", description = "传入area")
	public R submit(@Valid @RequestBody Area area) {
		return R.status(areaService.saveOrUpdate(area));
	}


	/**
	 * 删除 区域表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "逻辑删除", description = "传入ids")
	public R remove(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
		return R.status(areaService.deleteLogic(Func.toLongList(ids)));
	}

}
